﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.Common;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Linq;

namespace Lswweb.Controls
{
    public class LogisticsExpressionsEdit : SkinnedWebControl
    {
        private TreeViewDropDownList cities;
        private PlaceHolder expressionsPanel;
        private CheckBox cbxEnableOnlinePayment;
        private IButtonControl btnConfirm;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            cities = skin.FindControl<TreeViewDropDownList>("cities");
            expressionsPanel = skin.FindControl<PlaceHolder>("expressionsPanel");
            cbxEnableOnlinePayment = skin.FindControl<CheckBox>("cbxEnableOnlinePayment");
            btnConfirm = (IButtonControl)skin.FindControl("btnConfirm");

            cities.AutoPostBack = true;
            cities.SelectedIndexChanged += new EventHandler(cities_SelectedIndexChanged);
            btnConfirm.Click += new EventHandler(btnConfirm_Click);
            skin.Load += new EventHandler(skin_Load);

            if (Id > 0)
            {
                cbxEnableOnlinePayment.Checked = AreaExpressDeliveryExpressions.GetEnableOnlinePayment(Id);

                IList<LogisticsAccountingInfo> expressions = AreaExpressDeliveryExpressions.GetLogisticsExpressions(Id);
                LogisticsCompanyServices.FindAll().ForEach((info, index) => CreateExpressionControl(info, index, expressions));
            }
        }

        void skin_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            if (Id == 0)
            {
                cities_SelectedIndexChanged(cities, EventArgs.Empty);
            }
            else
            {
                cities.SelectedValue = Id;
            }
        }

        void cities_SelectedIndexChanged(object sender, EventArgs e)
        {
            int cityId = cities.SelectedValue;
            Page.Response.Redirect(Page.Urls.LogisticsExpressionsEdit(cityId),true);   
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            int cityId=cities.SelectedValue;
            bool enableOnlinePayment = cbxEnableOnlinePayment.Checked;

            IList<LogisticsAccountingInfo> logisticsAccountingGroup = new List<LogisticsAccountingInfo>();
            foreach(Control control in expressionsPanel.Controls)
            {
                foreach (Control child in control.Controls)
                {

                    TextBox tbx = child as TextBox;
                    if (tbx != null)
                    {
                        logisticsAccountingGroup.Add(new LogisticsAccountingInfo
                        {
                            CityId = cityId,
                            LogisticsCompanyId = tbx.ID.As<int>(),
                            Expressions = tbx.GetValue<string>()
                        });
                    }
                }
            }

            AreaExpressDeliveryExpressions.Update(cityId, enableOnlinePayment, logisticsAccountingGroup);
        }

        private void CreateExpressionControl(LogisticsCompanyInfo company, int index, IList<LogisticsAccountingInfo> expressions)
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            div.Attributes.Add("style", "padding-bottom:5px;");

            TextBox tbx=new TextBox();
            tbx.ID=company.Id.ToString();
            tbx.TextMode = TextBoxMode.MultiLine;
            tbx.Rows = 3;
            tbx.Width = 350;

            LogisticsAccountingInfo entity = expressions.FirstOrDefault(info => info.LogisticsCompanyId == company.Id);
            if(entity!=null)
                tbx.Text = entity.Expressions;

            Label lb = new Label();
            lb.Text = company.CompanyName + "<br />";

            div.Controls.Add(lb);
            div.Controls.Add(tbx);

            expressionsPanel.Controls.Add(div);
        }

        protected override string DefaultSkinFilename
        {
            get { return "Logistics/LogisticsExpressionsEdit.ascx"; }
        }

        private int Id
        {
            get { return SiteContext.Current.Id; }
        }
    }
}
